package com.wendy.admin.controller;

import com.wendy.admin.domain.bo.SysPostBo;
import com.wendy.admin.domain.vo.SysPostExcelVo;
import com.wendy.admin.domain.vo.SysPostVo;
import com.wendy.admin.entity.SysPost;
import com.wendy.admin.errorcode.SysPostError;
import com.wendy.admin.service.ISysPostService;
import com.wendy.common.core.domain.TableDataInfo;
import com.wendy.common.log.annotation.Log;
import com.wendy.common.log.enums.BusinessType;
import com.wendy.common.web.controller.BaseController;
import com.wendy.custom.security.annotation.RequiresPermissions;
import com.wendy.custom.security.utils.SecurityUtils;
import com.wendy.excel.ruoyi.utils.ExcelUtil;
import com.wendy.utils.spring.MapstructUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 岗位信息操作处理
 *
 * @author wendy
 */
@RestController
@RequestMapping("/post")
public class SysPostController extends BaseController {
    @Autowired
    private ISysPostService postService;

    /**
     * 获取岗位列表
     */
    @RequiresPermissions("system:post:list")
    @GetMapping("/page")
    public TableDataInfo page(SysPostBo post) {
        TableDataInfo<SysPostVo> list = postService.selectPostPage(post);
        return list;
    }

    @Log(title = "岗位管理", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:post:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysPostBo post) {
        List<SysPostVo> list = postService.selectPostList(post);
        List<SysPostExcelVo> result = MapstructUtils.convert(list, SysPostExcelVo.class);
        ExcelUtil<SysPostExcelVo> util = new ExcelUtil<SysPostExcelVo>(SysPostExcelVo.class);
        util.exportExcel(response, result, "岗位数据");
    }

    /**
     * 根据岗位编号获取详细信息
     */
    @RequiresPermissions("system:post:query")
    @GetMapping(value = "/{postId}")
    public SysPostVo getInfo(@PathVariable Long postId) {
        return MapstructUtils.convert(postService.selectPostById(postId), SysPostVo.class);
    }

    /**
     * 新增岗位
     */
    @RequiresPermissions("system:post:add")
    @Log(title = "岗位管理", businessType = BusinessType.INSERT)
    @PostMapping
    public int add(@Validated @RequestBody SysPost post) {
        if (!postService.checkPostNameUnique(post)) {
            SysPostError.ADD_ERROR.formatFire(post.getPostName());
        } else if (!postService.checkPostCodeUnique(post)) {
            SysPostError.ADD_CODE_ERROR.formatFire(post.getPostName());
        }
        post.setCreateBy(SecurityUtils.getUsername());
        return postService.insertPost(post);
    }

    /**
     * 修改岗位
     */
    @RequiresPermissions("system:post:edit")
    @Log(title = "岗位管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public int edit(@Validated @RequestBody SysPost post) {
        if (!postService.checkPostNameUnique(post)) {
            SysPostError.EDIT_ERROR.formatFire(post.getPostName());
        } else if (!postService.checkPostCodeUnique(post)) {
            SysPostError.EDIT_CODE_ERROR.formatFire(post.getPostName());
        }
        post.setUpdateBy(SecurityUtils.getUsername());
        return postService.updatePost(post);
    }

    /**
     * 删除岗位
     */
    @RequiresPermissions("system:post:remove")
    @Log(title = "岗位管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{postIds}")
    public int remove(@PathVariable Long[] postIds) {
        return postService.deletePostByIds(postIds);
    }

    /**
     * 获取岗位选择框列表
     */
    @GetMapping("/optionselect")
    public  List<SysPostVo> optionselect() {
        List<SysPost> posts = postService.selectPostAll();
        return MapstructUtils.convert(posts, SysPostVo.class);
    }
}
